package com.hencoderplus.demo1;

import android.Manifest;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;
import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "Endian";
    private RecordThread recordThread ;
    private PermissionsManager mPermissionsManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        init();
    }

    private void init() {
         mPermissionsManager = new PermissionsManager(this) {
            @Override
            public void authorized(int requestCode) {
                Toast.makeText(getApplicationContext(), "权限通过！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void noAuthorization(int requestCode, String[] lacksPermissions) {

            }

            @Override
            public void ignore() {

            }
        };
         String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.
         permission.RECORD_AUDIO};
        mPermissionsManager.checkPermissions(0,PERMISSIONS);

        findViewById(R.id.start_imagetext).setOnClickListener(this);
        findViewById(R.id.stop_cicletext).setOnClickListener(this);
        recordThread = new RecordThread();



    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mPermissionsManager.recheckPermissions(requestCode, permissions, grantResults);
    }
private MyAudioRecord myAudioRecord;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start_imagetext:
                Log.i(TAG, "onClick: begin");
               myAudioRecord =  new MyAudioRecord(true);
                new Thread(myAudioRecord).start();
                break;
            case R.id.stop_cicletext:
                Log.i(TAG, "onClick: stop");
                myAudioRecord.stopAudioRecord();
                break;
        }
    }

    class RecordThread extends Thread{
        private ByteBuffer byteBuffer;
        AudioRecord audioRecord;
        int bufferSize;
        private boolean isRun = false;

        //采样频率

        private static final int frequency = 32000;

        //采样深度

        private short audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

        //采样通道

        private static final short channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
        @Override
        public void run() {
            super.run();
            byte[] buffer = new byte[bufferSize];
            audioRecord.startRecording();
            while (isRun){
                audioRecord.read(buffer,0,bufferSize);
                Log.i(TAG, "run: 开始录制");
                try {
                    FileUtil.saveFile2SDCard(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Lzw_Audio/lzw",buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            audioRecord.stop();
        }

        public RecordThread() {
            bufferSize = AudioRecord.getMinBufferSize(frequency,channelConfiguration,audioEncoding);
            Log.i(TAG, "init: AudioRecord.getMinBufferSize="+bufferSize);
            byteBuffer = ByteBuffer.allocate(bufferSize);
            audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,frequency,channelConfiguration,audioEncoding,bufferSize);

        }

        public void startRecord(boolean isRecod){
            isRun = true;
        }
        public void stopRecord(){
            isRun = false;
            audioRecord.release();
        }
    }
}
